﻿@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


广播内容: 
<input type="text" id="content" /><br />
消息记录：
        <div id="divResult"></div>
<input type="button" id="btnSend" value="广播" />


<script>

$(document).ready(function () {
    function send() {
        //向comet_broadcast.asyn发送请求，消息体为文本框content中的内容，请求接收类为AsnyHandler
        $.post("/comet_broadcast.asyn", { content: $("#content").val() });
        
        //清空内容
        $("#content").val("");
    }

    function wait() {
        $.post("/comet_broadcast.asyn", { content: "-1" },
         function (data, status) {
             var result = $("#divResult");
             result.html(result.html() + "<br/>" + data);
             
             //服务器返回消息,再次立连接
             wait();
         }, "html"
         );
    }

    //初始化连接
    wait();

    $("#btnSend").click(function () { send(); });
    $("#content").keypress(function (event) {
        if (event.keyCode == 13) {
            send();
        }
    });
});
</script>